package com.gonsin.gview.logic.services.data;

import com.gonsin.gview.annotations.GContext;
import com.gonsin.gview.annotations.interfaces.IDataPicker;
import com.gonsin.gview.annotations.interfaces.IListDataPicker;
import com.gonsin.gview.annotations.interfaces.ITableDataPicker;
import com.gonsin.gview.logic.model.DataRequest;
import com.gonsin.gview.model.struct.EnumItem;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;
import java.util.List;

public abstract class GDataManager {

    private String from = "";

    private String dataManagerType;

    private String metaData;

    protected GDataManager(String dataManagerType, String metaData){
        this.dataManagerType = dataManagerType;
        this.metaData = metaData;
    }

    /**
     * 初始化数据处理器
     * @return
     */
    public static GDataManager initForSql(GContext context, String dataId, String sql){
        GSqlDataManager manager = new GSqlDataManager(context, dataId, sql, false);
        return manager;
    }

    /**
     * 初始化单体sql查询语句
     */
    public static GDataManager initForSingleSql(GContext context, String dataId, String sql) {
        GSqlDataManager manager = new GSqlDataManager(context, dataId, sql, true);
        return manager;
    }


    /**
     * 初始化数据处理器
     * @return
     */
    public static GDataManager initForJson(GContext context, String dataId, String json, String titleName){
        GJsonDataManager manager = new GJsonDataManager(context, dataId, json, titleName);
        return manager;
    }


    /**
     * 初始化数据处理器
     * @return
     */
    public static GDataManager initForPicker(GContext context, String dataId, IDataPicker picker){
        GDataPickerManager manager = new GDataPickerManager(context, dataId, picker);
        return manager;
    }


    /**
     * 初始化列表数据处理器
     */
    public static GDataManager initForListDataPicker(GContext context, String dataId, IListDataPicker picker) {
        GListDataPickerManager manager = new GListDataPickerManager(context, dataId, picker);
        return manager;
    }


    /**
     * 初始化表格数据
     */
    public static GDataManager initForTableDataPicker(GContext context, String dataId, ITableDataPicker picker) {
        GTableDataPickerManager manager = new GTableDataPickerManager(context, dataId, picker);
        return manager;
    }

//    public static GDataManager initForTreeDataPicker(GContext context, String dataId, ITreeDataPicker picker) {
//        GTreeDataPickerManager manager = new GTreeDataPickerManager(context, dataId, picker);
//        return manager;
//    }

    public static GDataManager initForTableDataDelegate(GContext context, String dataId, Object delegateService, Method sourceMethod) {
        DelegateTableDataPicker manager = new DelegateTableDataPicker(context, dataId, delegateService, sourceMethod);
        return manager;
    }


    /**
     * 初始化List数据
     */
    public static GDataManager initForListDataDelegate(GContext context, String dataId, Object delegateService, Method sourceMethod) {
        DelegateListDataPicker manager = new DelegateListDataPicker(context, dataId, delegateService, sourceMethod);
        return manager;
    }

    /**
     * 初始化单体数据
     */
    public static GDataManager initForSingleDataDelegate(GContext context, String dataId, Object delegateService, Method sourceMethod) {
        DelegateSingleDataPicker manager = new DelegateSingleDataPicker(context, dataId, delegateService, sourceMethod);
        return manager;
    }

    public static GDataManager initForEnums(GContext context, String dataId, List<EnumItem> enumItems) {
        GEnumsDataManager manager = new GEnumsDataManager(context, dataId, enumItems);
        return manager;
    }

    /**
     * 获取数据
     */
    public abstract Object generalData(HttpServletRequest request, HttpServletResponse response, DataRequest dataRequest) throws Throwable;

    public String getDataManagerType() {
        return dataManagerType;
    }

    public String getMetaData() {
        return metaData;
    }
}
